package cn.xuchunh.campusbbs.ui.fragments;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.adapter.CommunityAdapter;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.base.BaseRefreshListFragment;
import cn.xuchunh.campusbbs.bean.Forum;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.ui.activities.ForumActivity;
import cn.xuchunh.campusbbs.utils.JsonParser;
import cn.xuchunh.campusbbs.viewholder.ForumViewHolder;

import static android.support.v7.widget.LinearLayoutManager.VERTICAL;

/**
 * 社区
 * Created by XuChunH on 2015/9/12.
 */
public class CommunityFragment extends BaseRefreshListFragment<ForumViewHolder, Forum> {

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new GridLayoutManager(getActivity(), 2, VERTICAL, false);
    }

    @Override
    protected BaseRecyclerViewAdapter<ForumViewHolder, Forum> createAdapter() {
        return new CommunityAdapter(getActivity());
    }

    @Override
    protected List<Forum> parse(JSONObject jsonObject) throws JSONException {
        return JsonParser.parseForumList(jsonObject);
    }


    @Override
    protected void loadDataFromServer() {
        HttpRequestManager.getInstance(AppContext.getInstance())
                .loadForumList(new ListXJsonListener(), new SimpleErrorListener(), getVolleyTag());
    }

    @Override
    protected void loadDataFromCache() {

    }

    @Override
    public String getVolleyTag() {
        return CommunityFragment.class.getSimpleName();
    }

    @Override
    public String getCacheKey() {
        return null;
    }

    @Override
    public void onItemClick(int position, View item) {
        ForumActivity.launch(getActivity(), getAdapter().getItem(position));
    }
}
